package trinidad;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;

import java.awt.*;
import java.io.*;
import java.net.*;

public class GameRules extends JFrame {

	private static final long serialVersionUID = 1L;

	private JEditorPane html;

	private JPanel panel;

	private int PREFERRED_WIDTH = 700;

	private int PREFERRED_HEIGHT = 600;

	public GameRules(JFrame parent) {
		super("Trynidad - zasady gry");
		this.setIconImage(new ImageIcon(
				"grafx/toolbarButtonGraphics/general/Help16.gif").getImage());

		try {
			File currentDirectory;
			currentDirectory = (new File(".")).getCanonicalFile();

			File f = new File(currentDirectory + "/doc/rules.html");
			URL url = f.toURL();

			if (url != null) {
				panel = new JPanel(new BorderLayout());
				html = new JEditorPane(url);
				html.setEditable(false);
				html.addHyperlinkListener(createHyperLinkListener());

				JScrollPane scroller = new JScrollPane();
				JViewport vp = scroller.getViewport();
				vp.add(html);
				panel.add(scroller, BorderLayout.CENTER);
				panel.setVisible(true);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
		this.setContentPane(panel);

		this.setVisible(true);
	}

	public HyperlinkListener createHyperLinkListener() {
		return new HyperlinkListener() {
			public void hyperlinkUpdate(HyperlinkEvent e) {
				if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
					if (e instanceof HTMLFrameHyperlinkEvent) {
						((HTMLDocument) html.getDocument())
								.processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent) e);
					} else {
						try {
							html.setPage(e.getURL());
						} catch (IOException ioe) {
							System.out.println("IOE: " + ioe);
						}
					}
				}
			}
		};
	}

}